<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-item label="耳标号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag type="list" v-decorator="['earLabel',validatorRules.earLabel]" :trigger-change="true" dict="rpm_reg_prgancyregistry,distinct ear_label,ear_label" placeholder="请选择耳标号" :disabled="edit_disabled"/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="产犊日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date-limit  placeholder="请选择产犊日期" v-decorator="['calvingDate',validatorRules.calvingDate]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" :disabled="edit_disabled"  />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="产犊类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['calvingType',validatorRules.calvingType]" :trigger-change="true" dictCode="claving_type" placeholder="请选择产犊类型"  />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="产犊描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" @change="onChageMode" v-decorator="['calvingDescription',validatorRules.calvingDescription]" :trigger-change="true" dictCode="calving_desc_type" placeholder="请选择产犊描述" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="产犊数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['calvingNumber']" placeholder="请输入产犊数" style="width: 100%" disabled/>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="产犊难易度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['calvingDifficulty',validatorRules.calvingDifficulty]" :trigger-change="true" dictCode="calving_diff_level" placeholder="请选择产犊难易度" />
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="是否见胎" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-switch v-decorator="['isFetusvisible',validatorRules.isFetusvisible]"  ></j-switch>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="助产/难产原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['causeofMidwiferyOrDystocia']" placeholder="请输入助产/难产原因" ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-item label="操作员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['operatorId',validatorRules.operatorId]" :trigger-change="true" dictCode="sys_user,realname,username" placeholder="请选择操作员" />
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="犊牛管理关系表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="rpmRegCalfmanagerelationTable.loading"
            :columns="rpmRegCalfmanagerelationTable.columns"
            :dataSource="rpmRegCalfmanagerelationTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>

      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'RpmRegCalvingregistryModal',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 0,
        validatorRules: {
          earLabel: {
            rules: [
              { required: true, message: '请选择耳标号!'},
            ]
          },
          calvingDate: {
            rules: [
              { required: true, message: '请选择产犊日期!'},
            ]
          },
          calvingDescription: {
            rules: [
              { required: true, message: '请选择产犊描述!'},
            ]
          },
          operatorId: {
            rules: [
              { required: true, message: '请选择操作员!'},
            ]
          },
          calvingType: {
            rules: [
              { required: true, message: '请选择产犊类型!'},
            ]
          },
          calvingDifficulty: {
            rules: [
              { required: true, message: '请选择产犊难易度!'},
            ]
          },
          isFetusvisible: {
            rules: [
              { required: true, message: '是否见胎!'},
            ]
          },
        },
        edit_disabled:false,
        refKeys: ['rpmRegCalfmanagerelation', ],
        tableKeys:['rpmRegCalfmanagerelation', ],
        activeKey: 'rpmRegCalfmanagerelation',
        // 犊牛管理关系表
        rpmRegCalfmanagerelationTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '犊牛耳标',
              key: 'calfEarLabel',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '存放牛舍',
              key: 'inHouse',
              type: FormTypes.select,
              dictCode:"cowshed,name,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '出生体重',
              key: 'birthWeight',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '是否活犊',
              key: 'izLife',
              type: FormTypes.select,
              dictCode:"yn",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '性别',
              key: 'calfSex',
              type: FormTypes.select,
              dictCode:"cattle_sex",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '是否留养',
              key: 'izLive',
              type: FormTypes.select,
              dictCode:"yn",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '花色',
              key: 'color',
              type: FormTypes.select,
              dictCode:"cattle_color",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '品种',
              key: 'variety',
              type: FormTypes.select,
              dictCode:"cattle_varieties",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '健康情况',
              key: 'calfHealthy',
              type: FormTypes.select,
              dictCode:"calf_health_status",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '操作员',
              key: 'operatorId',
              type: FormTypes.select,
              dictCode:"sys_user,realname,username",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/rpm/rpmRegCalvingregistry/add",
          edit: "/rpm/rpmRegCalvingregistry/edit",
          rpmRegCalfmanagerelation: {
            list: '/rpm/rpmRegCalvingregistry/queryRpmRegCalfmanagerelationByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.edit_disabled =true;
        let fieldval = pick(this.model,'earLabel','calvingDate','calvingType','calvingDescription','calvingNumber','calvingDifficulty','isFetusvisible','causeofMidwiferyOrDystocia','operatorId')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })

        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.rpmRegCalfmanagerelation.list, params, this.rpmRegCalfmanagerelationTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          rpmRegCalfmanagerelationList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'earLabel','calvingDate','calvingType','calvingDescription','calvingNumber','calvingDifficulty','isFetusvisible','causeofMidwiferyOrDystocia','operatorId'))
     },
      onChageMode(checkedValues){
        // console.log( checkedValues)
        if (checkedValues.toString()==="1"||checkedValues.toString()==="2"){//单公胎和单母胎
          this.form.setFieldsValue({
            calvingNumber:1
          })
        }else if(checkedValues.toString()==="3"||checkedValues.toString()==="4"||checkedValues.toString()==="6"||checkedValues.toString()==="7"){
          this.form.setFieldsValue({
            calvingNumber:3
          })
        }else if(checkedValues.toString()==="5"||checkedValues.toString()==="8"||checkedValues.toString()==="9"){//
          this.form.setFieldsValue({
            calvingNumber:2
          })
        }
      },
      /** 当点击新增按钮时调用此方法 */
      add() {
       return new Promise((resolve) => {
          this.tableReset();
          resolve();
        }).then(() => {
          this.edit({})
         this.edit_disabled =false;
        })
      },
    }
  }
</script>

<style scoped>
</style>